<template>
  <div class="container">
    <div class="main-s">
      <div class="movie border-shadow">
        <div class="title-wrapper">
          <div class="poster">
            <img :src="info.poster" alt="" />
          </div>
          <div class="title-block">
            <div class="movie-title-s">
              <div class="movie-title-text">{{ info.name }}</div>
            </div>
            <div class="movie-info-s">
              <span class="labels"
                ><span class="rating"
                  ><span class="rating-number">{{ info.score }}</span></span
                ><span
                  class="label"
                  v-for="(tag, index) in info.tags"
                  :key="index"
                  ><span class="label-name">{{ tag }}</span></span
                >
              </span>
            </div>
          </div>
        </div>
        <div class="summary"><div></div></div>
      </div>
      <div class="search-result-container">
        <ul class="search-result-list">
          <div>
            <a
              v-for="(source, index) in info.source"
              :key="index"
              :href="source.url"
              target="_blank"
              rel="noreferrer"
              ><li class="search-result-item">
                <div class="content">
                  <div class="info">
                    <div class="title" v-html="source.name"></div>
                    <div class="tags">
                      <div class="website">
                        <img :src="source.icon" width="13px" /><span
                          class="website-name"
                          >{{ source.title }}</span
                        >
                      </div>
                    </div>
                  </div>
                  <span class="type-logo iconfont icon-online"></span>
                </div></li
            ></a>
          </div>
        </ul>
      </div>
    </div>
    <div class="sidebar">
      <div id="boxFixed" :class="{ is_fixed: isFixed }">
        <div class="card border-shadow">
          <div class="card-content">
            <div class="movie-list-header">
              <span class="movie-list-title">相关推荐</span>
            </div>
            <div class="movie-list-body">
              <div
                class="movie-list-item-t"
                v-for="(recommend, index) in recommends"
                :key="index"
              >
                <a
                  :href="'/search?key=' + recommend.name"
                  target="_blank"
                  rel="noreferrer"
                  ><div class="movie-post-wrapper">
                    <div class="movie-post-lazyload"></div>
                    <img :src="recommend.poster" alt="" class="movie-post" />
                  </div>
                  <div class="movie-info-small">
                    <div class="movie-title-small">{{ recommend.name }}</div>
                  </div></a
                >
              </div>
            </div>
          </div>
        </div>
      </div>
      <div>
        <!-- <ul class="items">
          <a href="/about?page=联系合作"><li class="item link">联系合作</li></a
          ><a href="/about?page=侵权举报"><li class="item link">侵权举报</li></a
          ><a href="/about?page=免责声明"
            ><li class="item link">免责声明</li></a
          >
        </ul> -->
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "search",
  layout: "default",
  data() {
    return {
      isFixed: false,
      offsetTop: 0,
      info: {
        mid: "",
        name: "",
        poster: "",
        score: "",
        tags: [],
        source: [],
      },
      recommends: [],
    };
  },
  mounted() {
    window.addEventListener("scroll", this.initHeight);
    this.$nextTick(() => {
      //获取对象相对于版面或由 offsetTop 属性指定的父坐标的计算顶端位置
      this.offsetTop = document.querySelector("#boxFixed").offsetTop;
    });
    this.getSearch();
  },
  methods: {
    initHeight() {
      // 设置或获取位于对象最顶端和窗口中可见内容的最顶端之间的距离 (被卷曲的高度)
      var scrollTop =
        window.pageYOffset ||
        document.documentElement.scrollTop ||
        document.body.scrollTop;
      //如果被卷曲的高度大于吸顶元素到顶端位置 的距离
      this.isFixed = scrollTop > this.offsetTop ? true : false;
    },
    getSearch() {
      let self = this;
      this.$axios
        .$get("/main/api/search2/", {
          params: { k: this.$route.query.key, p: 1 },
        })
        .then((data) => {
          self.info = data;
          self.getRecommend();
        });
    },
    getRecommend() {
      let self = this;
      this.$axios
        .$get("/main/api/recommend/", {
          params: { mid: this.info.mid },
        })
        .then((data) => {
          self.recommends = data.recommend;
        });
    },
  },
  //回调中移除监听
  destroyed() {
    window.removeEventListener("scroll", this.handleScroll);
  },
};
</script>
<style scoped>
.container {
  justify-content: center;
}
.is_fixed {
  position: fixed;
  top: 0;
  z-index: 999;
  width: 350px;
}
@media screen and (min-width: 800px) {
  .main-s {
    width: 610px;
    padding: 10px;
  }
}
.border-shadow {
  box-shadow: 0 8px 33px 0 rgb(30 35 42/5%);
  border-radius: 8px;
  background: #fff;
  box-sizing: border-box;
}
.title-wrapper {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 145px;
}
.poster {
  width: 100px;
  height: 100%;
  border-top-left-radius: 8px;
  flex-shrink: 0;
  overflow: hidden;
}
.poster img {
  width: 100%;
  height: 100%;
  -o-object-fit: cover;
  object-fit: cover;
  display: inline-block;
}
.title-block {
  display: flex;
  flex-direction: column;
  justify-content: center;
  width: 100%;
  height: 100%;
  border-bottom: 1px solid rgba(30, 35, 42, 0.05);
}
.movie-title-s {
  padding: 10px 10px 10px 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  box-sizing: border-box;
}
@media screen and (min-width: 800px) {
  .movie-title-text {
    font-size: 25px;
    font-weight: 700;
    line-height: 31px;
  }
}
.movie-info-s {
  padding: 10px 10px 10px 20px;
  color: #8590a6;
  font-size: 14px;
  line-height: 20px;
  display: inline-block !important;
}
.label,
.labels {
  display: flex;
  align-items: center;
}
.labels {
  line-height: 1;
  margin-top: 9px;
  white-space: nowrap;
  overflow-x: auto;
  overflow-y: hidden;
  overflow: auto hidden;
}
.rating {
  display: inline-block;
  font-size: 13px;
  font-weight: 700;
  padding: 3px 9px;
  line-height: 16px;
  margin-right: 8px;
  background: #0d253f;
  border-radius: 16px;
}
.label {
  padding: 3px 7px;
  margin-right: 8px;
  border-radius: 16px;
  color: rgba(0, 0, 0, 0.9);
  background: #e9eef5;
}
.summary {
  line-height: 20px;
  text-align: justify;
  color: #8590a6;
  padding: 15px;
  font-size: 14px;
}
.search-result-container,
.sort {
  margin-top: 20px;
}
.search-result-list {
  width: 100%;
  box-shadow: 0 4px 15px 0 rgb(30 35 42/5%);
}
.search-result-item {
  cursor: pointer;
  padding: 12px 12px 0;
  background: #fff;
}
.content {
  width: 100%;
  display: flex;
  border-bottom: 1px solid #f2f4f6;
  padding-bottom: 12px;
}
.info {
  width: calc(100% - 50px);
}
.title {
  font-size: 18px;
  line-height: 1.2;
  color: #175199;
  cursor: pointer;
  display: inline-block;
}
.search-result-list .title em {
  color: #dd5325;
  font-style: normal;
}
.tags {
  line-height: 1;
  font-size: 14px;
  margin-top: 9px;
}
.tags,
.website {
  display: flex;
  align-items: center;
}
.website {
  padding: 3px 6px;
  border-radius: 14px;
  color: rgba(0, 0, 0, 0.9);
  background: #e9eef5;
}
img {
  border-style: none;
}
.website-name {
  font-size: 12px;
  height: 14px;
  line-height: 14px;
  padding-left: 3px;
}
.type-logo {
  width: 50px;
  font-size: 30px;
  color: #8590a6;
  display: flex;
  align-items: center;
}
@media screen and (min-width: 800px) {
  .sidebar {
    box-sizing: border-box;
    width: 350px;
  }
  .card {
    margin-top: 15px;
    width: 100%;
    min-height: 0;
    padding: 5px 10px 10px;
    box-sizing: border-box;
  }
  .movie-list-item-t {
    width: 31.333333333333336%;
    padding: 10px 1% 3px;
    font-size: 15px;
    cursor: pointer;
    border-radius: 8px;
  }
  .movie-title-small {
    font-size: 13px;
    font-weight: 700;
    line-height: 1.5;
    color: rgba(0, 0, 0, 0.9);
    margin-top: 4px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  .items {
    list-style: none;
    display: flex;
    flex-wrap: wrap;
    line-height: 1.5;
    padding: 0;
    margin-top: 6px;
    margin-left: 12px;
  }
  .item {
    font-size: 13px;
    color: #8590a6;
    margin-right: 15px;
  }
  .link {
    cursor: pointer;
  }
}
@media screen and (max-width: 800px) {
  .movie-title-text {
    max-width: 200px;
    font-size: 25px;
    font-weight: 700;
    line-height: 31px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  .sidebar {
    display: none;
  }
}
.rating-number {
  color: #fdab3e;
}
.card-content {
  box-sizing: border-box;
}
.movie-list-header {
  width: 100%;
  margin: 0 -10px;
  padding: 0 10px;
  background: hsla(0, 0%, 100%, 0.9);
  -webkit-backdrop-filter: saturate(180%) blur(30px);
  backdrop-filter: saturate(180%) blur(30px);
}
.movie-list-title {
  font-size: 20px;
  font-weight: 700;
  line-height: 2;
}
.movie-list-body {
  display: flex;
  flex-wrap: wrap;
  background: none;
  line-height: 100%;
  margin: 0 -1% -8px;
  box-sizing: border-box;
}
.movie-post-wrapper {
  position: relative;
  overflow: hidden;
  color: transparent;
}
.movie-post-lazyload {
  padding-top: 140%;
  background: url()
    50% / cover no-repeat;
  border-radius: 8px;
}
.movie-post {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  border-radius: 8px;
  -o-object-fit: cover;
  object-fit: cover;
}
</style>
